package com.hxcy.doki;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class HomeFragment extends BaseFragment {

    private WebView webView;
    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        webView = (WebView)view.findViewById(R.id.webView);

        WebSettings webSettings = webView.getSettings();

        webSettings.setJavaScriptEnabled(true);

        webView.addJavascriptInterface(new JsInterface(), "control");

        webView.loadUrl("file:///android_asset/interact.html");

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return false;
            }
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    public class JsInterface {

        @JavascriptInterface
        public void showToast(String toast) {
            /*Toast.makeText(getActivity(), toast, Toast.LENGTH_SHORT).show();
            //log("show toast success");
            log("http:www.baidu.com");
            Log.e(TAG, "showToast: "+toast );*/
            startActivity(new Intent(getActivity(),LoginActivity2.class));
        }

        public void log(final String msg){
            webView.post(new Runnable() {
                @Override
                public void run() {
                    //webView.loadUrl("javascript: log(" + "'" + msg + "'" + ")");
                    webView.loadUrl(msg);
                    Log.e(TAG, "run: "+msg);
                }
            });
        }
    }

}